﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace AllInOne
{
    /// <summary>
    /// 日志信息记录
    /// </summary>
    class SimpleConsole
    {
        public static object locker = new object();


        public  static void WriteInfo(string msg,object sender)
        {
            lock (locker)
            {
                Console.WriteLine(msg);
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";  //路径
                if (!Directory.Exists(filePath))  //不存在则创建
                {
                    Directory.CreateDirectory(filePath);
                }
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";  //文档路径
                try
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("**************************************************");
                        sw.WriteLine(string.Format("[Info][{0}][{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), sender == null ? "" : sender.GetType().ToString(), msg));
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
                catch (IOException e)
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("**************************************************");
                        sw.WriteLine(string.Format("[Error][{0}][{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), sender == null ? "" : sender.GetType().ToString(), e.Message));
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
            }
        }

        public static void WriteError(string msg, object sender)
        {
            lock (locker)
            {
                Console.WriteLine(msg);
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";  //路径
                if (!Directory.Exists(filePath))  //不存在则创建
                {
                    Directory.CreateDirectory(filePath);
                }
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";  //文档路径
                try
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("**************************************************");
                        sw.WriteLine(string.Format("[Error][{0}][{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), sender == null ? "" : sender.GetType().ToString(), msg));
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
                catch (IOException e)
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("**************************************************");
                        sw.WriteLine(string.Format("[Error][{0}][{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), sender == null ? "" : sender.GetType().ToString(), e.Message));
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
            }
        }

        /// <summary>
        /// 记录设备日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="sender"></param>
        public static void WriteOprateLog(string msg, object sender)
        {
            lock (locker)
            {
                Console.WriteLine(msg);
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "LogDevice\\";  //路径
                if (!Directory.Exists(filePath))  //不存在则创建
                {
                    Directory.CreateDirectory(filePath);
                }
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "LogDevice\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";  //文档路径
                try
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("**************************************************");
                        sw.WriteLine(string.Format("[Error][{0}][{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), sender == null ? "" : sender.GetType().ToString(), msg));
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
                catch (IOException e)
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("**************************************************");
                        sw.WriteLine(string.Format("[Error][{0}][{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), sender == null ? "" : sender.GetType().ToString(), e.Message));
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
            }
        }

        /// <summary>
        /// 退出设备日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="sender"></param>
        public static void WriteQuitLog(string msg, object sender)
        {
            lock (locker)
            {
                Console.WriteLine(msg);
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "QuitLog\\";  //路径
                if (!Directory.Exists(filePath))  //不存在则创建
                {
                    Directory.CreateDirectory(filePath);
                }
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "QuitLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";  //文档路径
                try
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("**************************************************");
                        sw.WriteLine(string.Format("[Error][{0}][{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), sender == null ? "" : sender.GetType().ToString(), msg));
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
                catch (IOException e)
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("**************************************************");
                        sw.WriteLine(string.Format("[Error][{0}][{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), sender == null ? "" : sender.GetType().ToString(), e.Message));
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
            }
        }
    }
}
